<template>
  <a-modal
    :title="title"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="true"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row :gutter="24">
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="name"
              label="模板名称">
              <a-input placeholder="请输入模板名称" v-model="model.name"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="status"
              label="状态">
               <j-dict-select-tag placeholder="请输入状态"
                                 v-model="model.status"
                                 dict-code="common_status"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="outPutPath"
              label="输出文件类型">
              <j-dict-select-tag placeholder="请选择输出文件类型"
                                 v-model="model.outPutPath"
                                 dict-code="generate_file_type"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="fileType"
              label="文件后缀类型">
              <j-dict-select-tag placeholder="请选择文件后缀类型"
                                 type="radio"
                                 v-model="model.fileType"
                                 dict-code="file_type"
                                 @change="fileTypeChange"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="文件名结尾">
              <a-input placeholder="请输入文件名结尾" v-model="model.suffixFileName" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :md="24" :sm="16">
            <a-form-item
              :labelCol="conentlabelCol"
              :wrapperCol="conentwrapperCol"
              label="模板内容">
              <div ref="main" style="width: 100%;height: 300px;margin-left: 5px;"></div>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import validator from "@/utils/validate";
  import * as monaco from 'monaco-editor'

  export default {
    name: "MoudualTemplateConfigModal",
    data() {
      return {
        title: "操作",
        visible: false,
        editor: null,
        curTheme: 'vs-dark',
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 8},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 12},
        },
        conentlabelCol: {
          xs: {span: 24},
          sm: {span: 4},
        },
        conentwrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        input_width: {width: '270px'},
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          name:[{required: true, message: '请输入模板名称!'},
                {max: 64, message: '长度不能大于64', trigger: 'blur'}],
          fileType:[
              {required: true, message: '请选择文件后缀类型!'}],
          outPutPath:[
              {required: true, message: '请选择输出文件类型!'}],
          systemId:[
              {required: true, message: '请选择所属框架类型!'}],
          templateContent:[
                {required: true, message: '请输入模板内容!'}],
          status: [{required: true, message: '模板状态必填!'}]
        },
        url: {
          add: "/generator/MoudualTemplateConfig/add",
          edit: "/generator/MoudualTemplateConfig/edit",
        }
        ,
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.visible = true;
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.model = Object.assign({}, record);
          if (this.model.templateContent) {
            this.initEditor(this.model.fileType, this.model.templateContent);
          } else {
            this.initEditor("java", "");
          }
          this.editor.onDidChangeModelContent((event) => {
            const newValue = this.editor.getValue();
            console.log(newValue)
          })
        })
      },
      close() {
        this.editorDestory();
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            this.model.templateContent = this.getValue();
            httpAction(httpurl, this.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      initEditor(type, value) {
        // 初始化编辑器，确保dom已经渲染
        this.editor = monaco.editor.create(this.$refs.main, {
          value: value,//编辑器初始显示文字
          language: type,//语言支持自行查阅demo
          automaticLayout: true,//自动布局
          theme: 'vs-dark' //官方自带三种主题vs, hc-black, or vs-dark
        });
      },
      fileTypeChange(value) {
        let content = this.getValue();
        this.editorDestory();
        this.initEditor(value, content);
      },
      getValue() {
        return this.editor.getValue(); //获取编辑器中的文本
      },
      editorDestory() {
        this.editor.dispose();
      }
    }
  }
</script>

<style lang="less" scoped>

</style>
